<?php

namespace Addons\Finance\Model;

use Think\Model;

/**
 * Class AddonFinance
 * @package Addons\AddonFinance\Model
 * @author:温作权
 */
class AddonFinanceModel extends \BaseModel
{
    protected $tableName = 'addon_finance';

    /*****添加**/
    public function add($row)
    {
        $row = $this->create($row);
        $row['add_date'] = date("Y-m-d H:i:s");
        $id = parent::add($row);
        return $id;
    }
    /*******查出****/
    //add [hours,week,day,month,year]
    public function get_count($time = "", $add = "day")
    {
        $where = "1";
        if ($time) {
            $start_date = date("Y-m-d H:i:s", $time);
            $end_date = date("Y-m-d H:i:s", strtotime("$start_date +1{$add}"));
            $where .= " and ('$start_date' < add_date  and add_date< '$end_date')";
        }

        $data = $this->where($where)->field("SUM(num) as count")->select();
        $r['count'] = $data[0]['count'] ? $data[0]['count'] : 0;
        $type_array = array('recharge' => '在线充值', 'subject' => '开通课程', 'ask' => '购买答题数');
        foreach ($type_array as $k => $one) {
            $sql = $where . " and (type='$k')";
            $data = $this->where($sql)->field("SUM(num) as count")->select();
            $r[$k] = $data[0]['count'] ? $data[0]['count'] : 0;
        }
        return $r;
    }

}
